import { Regex } from './index'
// 通用手机号校验程序
export const phoneValidator = (msgEmpty?:(undefined | string), msgInvalid?:(undefined | string)) => {
  msgEmpty = msgEmpty ? msgEmpty : '手机号不能为空!'
  msgInvalid = msgInvalid ? msgInvalid : '请输入合法的手机号！'
  return (rule:any, value:any, callback:Function) => {
    const val = value && value.trim()
    if (!val) {
      callback(msgEmpty)
      return
    }
    if (!Regex.phone.test(val)) {
      callback(msgInvalid)
      return
    }
    callback()
  }
}

// 通用身份证校验程序
export const idValidator = (msgEmpty:(undefined | string), msgInvalid:(undefined | string)) => {
  msgEmpty = msgEmpty ? msgEmpty : '身份证号不能为空!'
  msgInvalid = msgInvalid ? msgInvalid : '请输入合法的身份证号！'
  return (rule:any, value:any, callback:Function) => {
    const val = value && value.trim()
    if (!val) {
      callback(msgEmpty)
      return
    }
    if (!Regex.idCardNew.test(val)) {
      callback(msgInvalid)
      return
    }
    callback()
  }
}
